package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ErrorCode;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.api.roomlayout.vo.GetRoomLayoutByApartmentRespVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomLayoutMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomLayout;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class RoomLayoutRepository extends ServiceImpl<RoomLayoutMapper, RoomLayout> {
    private final RoomLayoutMapper roomLayoutMapper;

    public IPage<RoomLayout> page1(Integer pageSize, Integer pageNo, Long apartmentId, List<Long> ids, Boolean config) {
        Page<RoomLayout> page = Page.of(pageNo, pageSize);
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoomLayout::getId, RoomLayout::getApartmentId, RoomLayout::getName, RoomLayout::getMonthRental)
                .eq(apartmentId != null, RoomLayout::getApartmentId, apartmentId)
                .in(CollectionUtil.isNotEmpty(ids), RoomLayout::getId, ids)
                .isNull(config != null && !config, RoomLayout::getMonthRental)
                .isNotNull(config != null && config, RoomLayout::getMonthRental)
                .orderByDesc(RoomLayout::getId);
        page = this.page(page, wrapper);
        return page;
    }

    public List<RoomLayout> getByApartmentId(@NonNull Long apartmentId) {
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomLayout::getApartmentId, apartmentId);
        wrapper.orderByAsc(RoomLayout::getMonthRental);
        return this.list(wrapper);
    }

    public RoomLayout getByApartmentIdAndName(@NonNull Long apartmentId, @NonNull String name) {
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomLayout::getApartmentId, apartmentId);
        wrapper.eq(RoomLayout::getName, name);
        return this.getOne(wrapper);
    }

    public CommonResult<Boolean> delByApartmentId(@NonNull Long apartmentId) {
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomLayout::getApartmentId, apartmentId);
        this.remove(wrapper);
        return CommonResult.success(Boolean.TRUE);
    }

    public RoomLayout myGetById(Long layoutId) {
        RoomLayout roomLayout = this.getById(layoutId);
        if(ObjectUtil.isEmpty(roomLayout)){
            String msg = StrUtil.format("房型信息id{}不存在", layoutId);
            throw new ServiceException(new ErrorCode(30037 , msg));
        }
        return roomLayout;
    }

    public List<RoomLayout> getByApartmentIdAndFilter(Long apartmentId, Long layoutId) {
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomLayout::getApartmentId, apartmentId);
        wrapper.ne(RoomLayout::getId, layoutId);
        return this.list(wrapper);
    }

    public List<RoomLayout> getByApartmentIds(List<Long> apartmentIds) {
        LambdaQueryWrapper<RoomLayout> wrapper = Wrappers.lambdaQuery();
        wrapper.in(RoomLayout::getApartmentId, apartmentIds);
        wrapper.orderByAsc(RoomLayout::getMonthRental);
        return this.list(wrapper);
    }

    public List<GetRoomLayoutByApartmentRespVO> getRoomLayoutByApartments(List<Long> ids) {
        return roomLayoutMapper.getRoomLayoutByApartments(ids);
    }
}
